<?php defined('SYSPATH') or die('No direct script access.');

class Controller_BoDish extends Controller_Layout {
	public $auth_required = TRUE;
	private $modelName = "dish";

	public function action_index()
	{
		$dish = ORM::factory("dish");
		$result_dish = $dish->where("status","<>","I")->find_all();
		
	}
	
	public function action_insertPage(){
		$view = View::factory("pages/bo/dish_add_page");
		$view->dish_cate_id = $_GET["dish_cate_id"];
		exit($view->render());
	}

	public function action_insert(){
		$dish = ORM::factory("dish");
		$fileUtil = FileUtil::Factory();
		$fileName = $fileUtil->saveFile(Upload::$default_directory.DIRECTORY_SEPARATOR.'dish');
		if ($fileName != FALSE){
			$fileUtil->setFilePath($fileName,'picture');
		}
		$_POST["status"] = "A";
		if($dish->values($_POST)->check()){
			ORM::factory("supplier",$_POST["supplierid"])->updateModifyTime();
			$dish->save();
			$this->baseInfo($_POST["dish_cate_id"]);
		}
	}

	public function action_updatePage(){
		$dish = ORM::factory("dish",$_GET["dish_id"]);
		$view = View::factory("pages/bo/dish_edit_page");
		$view->result = $dish;
		exit($view->render());
	}
	
	public function action_update(){
		$dish = ORM::factory("dish",$_POST["id"]);
		$dish->name = $_POST["name"];
		$dish->price = $_POST["price"];
		$dish->price2 = $_POST["price2"];
		$dish->price3 = $_POST["price3"];
		$dish->desc = $_POST["desc"];
		
		$fileUtil = FileUtil::Factory();
		$fileUtil->setFilePath($fileUtil->saveFile(Upload::$default_directory.DIRECTORY_SEPARATOR.'dish'),'picture');
		if (!empty($_POST['picture'])){
			if ($_POST['picture']!=$dish->picture){
				if (!empty($dish->picture)){
					$fileUtil->deleteFile($dish->picture);
				}
				$dish->picture = $_POST['picture'];
			}
		}		
		
		if($dish->save()){
			ORM::factory("supplier",$_POST["supplierid"])->updateModifyTime();
			$this->baseInfo($_POST["dish_cate_id"]);
		}
		
	}
	
	public function action_copy(){
		$dish = ORM::factory("dish");
		$ids = $_REQUEST["ids"];
		foreach (split(",", $_POST["ids"]) as $id){
			if (!empty($id)){
				$dish->or_where("id","=",$id);
			}
		}
		$copyStatus = true;
		$source_dish_category = ORM::factory("dishCategory",$_REQUEST["targetdishcateid"]);
		$result_dish = $dish->find_all()->as_array();
		foreach ($result_dish as $dish){
			$dish_id = $dish->id;
			$new_dish = ORM::factory("dish");
			$new_dish = $this->cloneEntity($dish,$new_dish);
			$new_dish->dish_cate_id = $source_dish_category->id;
			$new_dish->supplierid = $source_dish_category->supplierid;
			$new_dish->copyfrom = $dish->id;
			if($new_dish->save()){
				$result_dish_item = ORM::factory("dishItem")->where("dishid","=",$dish_id)->and_where("status","<>","I")->find_all()->as_array();
				foreach ($result_dish_item as $dish_item){
					$new_dish_item = ORM::factory("dishItem");
					$new_dish_item = $this->cloneEntity($dish_item,$new_dish_item);
					$new_dish_item->dishid = $new_dish->id;
					$new_dish_item->dishcateid = $source_dish_category->id;
					$new_dish_item->supplierid = $source_dish_category->supplierid;
					$new_dish_item->copyfrom = $dish_item->id;
					if($new_dish_item->save()){
						
					}else{
						$copyStatus = false;
					}
				}
			}else{
				$copyStatus = false;
			}
		}
	} 
	
	private function cloneEntity($srcEntity,$newEntity){
		foreach ($srcEntity->list_columns() as $col) {
			$newEntity->__set($col["column_name"],$srcEntity->__get($col["column_name"]));
		}
		$newEntity->id = null;
		return $newEntity;
	}
	
	private function baseInfo($dish_cate_id){
		$dish = ORM::factory("dish");
		$result_dish = $dish->where("dish_cate_id","=",$dish_cate_id)->and_where("status","<>","I")->find_all();
		$view = View::factory("pages/bo/dish_page");
		$view->result_dish = $result_dish;
		$dish_category = ORM::factory("dishCategory",$dish_cate_id);
		$view->result_supplier_name = ORM::factory("supplier",$dish_category->supplierid)->displaySupplierName();
		$view->result_dish_cate_name = $dish_category->name;
		exit($view->render());
	}
	
	public function action_show(){
		$this->baseInfo($_GET["dish_cate_id"]);
	}
	
	public function action_deleteAll(){
		$dish = ORM::factory("dish");
		$dish->status = "I";
		$ids = $_POST["ids"];
		foreach (split(",", $_POST["ids"]) as $id){
			if (!empty($id)){
				$dish->or_where("id","=",$id);
				if (empty($supplierId)){
					$supplierId = ORM::factory("dish",$id)->supplierid;
				}
			}
		}
		if($dish->save_all()){
			ORM::factory("supplier",$supplierId)->updateModifyTime();
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "删除菜色成功";
			exit($view->render());
		}
	}
	
	public function action_removeCopyFlag(){
		$model = ORM::factory($this->modelName,$_REQUEST["id"]);
		$model->copyfrom = null;
		$model->save();
		
		$dish_items = ORM::factory("dishItem")->where("dishid","=",$_REQUEST["id"]);
		$dish_items->copyfrom = null;
		$dish_items->save_all();
	}
}
